package com.china08.yxyapi.api.home.kecheng.cloud;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.mg.lessons.YxyLessonsUserScore;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.repository.mg.YxyLessonsUserScoreRepository;

/**
 * 
 * @annotation: 云课程--修改课程评分
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class KechengCloudLessonMarkUpdController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsUserScoreRepository yxyLessonsUserScoreRepository;

    @Autowired
    private YxyLessonsCloudLessonRepository yxyLessonsCloudLessonRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/user/score/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {
        RespModel model = new RespModel();
        String userId = getProfile(request, response).getId();
        String lessonId = reqModel.lessonId;
        Integer mark = reqModel.myMark;
        String id = userId + lessonId;
        YxyLessonsUserScore yxyLessonsUserScores = yxyLessonsUserScoreRepository.findByUserIdAndLessonsId(userId,
                lessonId);
        Assert.isTrue(yxyLessonsUserScores == null, "您对此课程已经评价过了！");
        YxyLessonsUserScore userScore = new YxyLessonsUserScore();
        userScore.setId(id);
        userScore.setUserId(userId);
        userScore.setLessonsId(lessonId);
        userScore.setScore(mark);
        yxyLessonsUserScoreRepository.save(userScore);
        // 重新计算课程评分
        YxyLessonsCloudLesson cloudLesson = yxyLessonsCloudLessonRepository.findById(lessonId);
        if (cloudLesson != null) {
            List<YxyLessonsUserScore> lessonsUserScores = yxyLessonsUserScoreRepository.findByLessonsId(lessonId);
            Integer score1 = 0;
            for (YxyLessonsUserScore yxyLessonsUserScore : lessonsUserScores) {
                score1 += yxyLessonsUserScore.getScore();
            }
            // 计算总分存入课程表里
            Float ep = (float) score1 / (float) lessonsUserScores.size();
            BigDecimal b = new BigDecimal(ep);
            Float f1 = (float) b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            cloudLesson.setEp(f1);
            cloudLesson.setEpTimes(cloudLesson.getEpTimes() + 1);
            yxyLessonsCloudLessonRepository.save(cloudLesson);
        }
        model.setMessage("更新评分成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课程ID
        public String lessonId;
        // 我的评分
        public Integer myMark;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
